//
//  Release_SupplyChain.m
//  HappyStore
//
//  Created by 李小辉 on 16/11/24.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "Release_SupplyChain.h"
#import "ReleaseSupplyChainView.h"
#import "SelectedAddress.h"
#import "datePickView.h"
#import "SelectedIndustry.h"
@interface Release_SupplyChain ()<selectedAddressDelegate, selectedDateDelegate,selectedIndustryDelegate>
@property (assign, nonatomic) NSInteger type;
@property (strong, nonatomic) ReleaseSupplyChainView *supplyView;
@property (strong, nonatomic) SelectedAddress *selectAddress;//选择地区
@property (strong, nonatomic) NSArray *aryHYtitleAry;
@property (strong, nonatomic) SelectedIndustry *selectedIndustry;
@property (assign, nonatomic) NSInteger selectedInt;

@end

@implementation Release_SupplyChain


- (NSArray *)aryHYtitleAry{
    if (!_aryHYtitleAry) {
        _aryHYtitleAry = @[@"生物医药",@"旅游休闲",@"制造业",@"批发零售",
                        @"房地产业",@"交通运输",@"冶金矿业",@"建筑建材",
                        @"信息软件",@"金融",@"教育",@"现代服务业",
                        @"生态环保",@"市政基建",@"文体娱乐",@"社会保障"
                        ,@"水电燃气",@"农林牧渔",@"其他行业"];
    }
    return _aryHYtitleAry;
}
- (ReleaseSupplyChainView *)supplyView{
    if (!_supplyView) {
        _supplyView = [[[NSBundle mainBundle] loadNibNamed:@"ReleaseSupplyChainView" owner:self options:nil] lastObject];
    }
    return _supplyView;
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //导航条都透明度
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16.f],NSFontAttributeName,[UIColor whiteColor],NSForegroundColorAttributeName,nil]];

}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //导航条都透明度
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *segmentedAry = @[@"采购",@"供应"];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedAry];
    segmentedControl.frame = CGRectMake(0, 0, 120, 25);
    UIFont *font = [UIFont systemFontOfSize:14];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    [segmentedControl setTitleTextAttributes:dic forState:UIControlStateNormal];
    segmentedControl.selectedSegmentIndex = 0;
    segmentedControl.tintColor =  RGB(44, 123, 235);
    [segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segmentedControl;
    
    self.type = 2;
    self.view.backgroundColor = RGB(247, 247, 247);
    
    // 设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    //发布
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH)];

    [self.supplyView setFrame:CGRectMake(0, 0, self.supplyView.width, self.supplyView.height)];
    [scrollView addSubview:self.supplyView];
    scrollView.contentSize = CGSizeMake(screenW, self.supplyView.height);
    
    [self.view addSubview:scrollView];
    
    [self.supplyView.titleTextField addTarget:self action:@selector(eventAction:) forControlEvents:UIControlEventAllEvents];
    [self.supplyView.industryTextField addTarget:self action:@selector(eventAction:) forControlEvents:UIControlEventAllEvents];
    [self.supplyView.adressTextField addTarget:self action:@selector(eventAction:) forControlEvents:UIControlEventAllEvents];
    [self.supplyView.priceTextField addTarget:self action:@selector(eventAction:) forControlEvents:UIControlEventAllEvents];
    [self.supplyView.unitTextField addTarget:self action:@selector(eventAction:) forControlEvents:UIControlEventAllEvents];
    [self.supplyView.quantityTextField addTarget:self action:@selector(eventAction:) forControlEvents:UIControlEventAllEvents];
    [self.supplyView.stopTimeTextField addTarget:self action:@selector(eventAction:) forControlEvents:UIControlEventAllEvents];
    
    //通知:监听文字的改变
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self.supplyView.describeTextView];
//按钮选择
    UIButton *clicBtn = [self.supplyView viewWithTag:62];
    [clicBtn addTarget:self action:@selector(chioseAction:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *clicBtn1 = [self.supplyView viewWithTag:63];
    [clicBtn1 addTarget:self action:@selector(chioseAction:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *clicBtn2 = [self.supplyView viewWithTag:65];
    [clicBtn2 addTarget:self action:@selector(chioseAction:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *clicBtn3 = [self.supplyView viewWithTag:67];
    [clicBtn3 addTarget:self action:@selector(chioseAction:) forControlEvents:UIControlEventTouchUpInside];
    //发布
    [self.supplyView.btnClickReleaseBtn addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
}
//返回
- (void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)eventAction: (UITextField *)textField{

    UILabel *label = [self.supplyView viewWithTag:textField.tag - 30];
    if (textField.text.length == 0) {
        label.hidden = NO;
    }else{
        label.hidden = YES;
    }
}
- (void)textDidChange{
    UILabel *label = [self.supplyView viewWithTag:30];
    if (self.supplyView.describeTextView.text.length == 0) {
        label.hidden = NO;
    }else{
        label.hidden = YES;
    }
}
#pragma mark - segument
-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)control{
    if (control.selectedSegmentIndex == 0) {
        self.type = 2;
    }else{
        self.type = 1;
    }
}
//单位选择回调
- (void)selectedIndustry:(NSString *)industry{
    //类型
    if (self.selectedInt == 1) {
        self.supplyView.industryTextField.text = industry;
        UILabel *label = [self.supplyView viewWithTag:22];
        label.hidden = YES;
    }else{
        //单位
        self.supplyView.unitTextField.text = industry;
        UILabel *label = [self.supplyView viewWithTag:25];
        label.hidden = YES;
    }
}

//地址回调
- (void)seceltedRowSshowMsg:(NSString *)showMsg andProvince:(NSString *)strProvince andCity:(NSString *)strCity andDistrict:(NSString *)strDistrict
{
    self.supplyView.adressTextField.text = showMsg;
    UILabel *label = [self.supplyView viewWithTag:23];
    label.hidden = YES;
}

//时间回调
- (void)seceltedDate:(NSString *)date{
    self.supplyView.stopTimeTextField.text = date;//[date substringToIndex:10];
    UILabel *label = [self.supplyView viewWithTag:27];
    label.hidden = YES;
}
//选择类型回调
- (void)seceltedRow:(NSInteger)rrow{
    self.supplyView.industryTextField.text = self.aryHYtitleAry[rrow];
    UILabel *label = [self.supplyView viewWithTag:22];
    label.hidden = YES;
}
#pragma mark - 按钮选择
- (void)chioseAction:(UIButton *)btn{
    if (btn.tag == 62) {//类别
        self.selectedInt = 1;
        self.selectedIndustry = [SelectedIndustry sharedSelectedIndustry];
        self.selectedIndustry.delegate = self;
        [self.selectedIndustry btnClickNumStr:@"1"];
        
    }else if (btn.tag == 63){//地区
        self.selectAddress = [SelectedAddress sharedSelectedAddress];
        self.selectAddress.delegatee = self;
        [self.selectAddress btnClickAddress];
    }else if (btn.tag == 65){//单位
        self.selectedInt = 2;
        self.selectedIndustry = [SelectedIndustry sharedSelectedIndustry];
        self.selectedIndustry.delegate = self;
        [self.selectedIndustry btnClickNumStr:@"2"];
    }else{//截止时间
        datePickView *dateee = [datePickView sharedSelectedAddress];
        dateee.delegatee = self;
        [dateee btnClickDate];
    }
    
}
#pragma mark - 发布
- (void)rightAction{
//    3.发布供应采购链
//http://192.168.1.188:8080/lensum/addSupply_app_chain.im.lensum?token=EhnDuxqM6n8u9OIIuwcT&type=1&userId=10079&title=title&industry=hangye&address=dizhi&money=1000&unit=danwei&number=1000&endTime=2016-11-21&content=neirong
//    参数说明：type=类型 1-供应 2-采购&userId=10079&title=标题&industry=行业&address=地址&money=金额&unit=单位&number=数量&endTime=截止时间-格式：2016-11-21&content=内容       files-为文件
    BOOL isOK = YES;
    
    if (isOK && self.supplyView.titleTextField.text.length == 0) {
        isOK = NO;
        [self showSingleIconBubble:@"请输入标题"];
    }
    if (isOK && self.supplyView.industryTextField.text.length == 0) {
        isOK = NO;
        [self showSingleIconBubble:@"请选择产品类型"];
    }
    if (isOK && self.supplyView.adressTextField.text.length == 0) {
        isOK = NO;
        [self showSingleIconBubble:@"请选择项目地区"];
        
    }
    if (isOK && self.supplyView.unitTextField.text.length == 0) {
        isOK = NO;
        
       [self showSingleIconBubble:@"请选择单位"];
    }
    if (isOK && self.supplyView.quantityTextField.text.length == 0) {
        isOK = NO;

        [self showSingleIconBubble:@"请填写单位"];
    }
    if (isOK && self.supplyView.stopTimeTextField.text.length == 0) {
        isOK = NO;

        [self showSingleIconBubble:@"请选择时间"];
    }
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *nowTime = [NSDate date];
    //        nowTime = [dateFormatter dateFromString:];
    NSDate *endTime = [dateFormatter dateFromString:self.supplyView.stopTimeTextField.text];
    
    NSComparisonResult *result = [nowTime compare:endTime];
    if (isOK && result != NSOrderedAscending) {
        isOK = NO;

        [self showSingleIconBubble:@"时间已过期"];
    }
    if (isOK && self.supplyView.describeTextView.text.length == 0) {
        isOK = NO;

        [self showSingleIconBubble:@"请输入详细描述"];
    }
    
    if (isOK) {
        NSMutableDictionary *muDic = [NSMutableDictionary dictionary];
        muDic[@"type"] = @(self.type);
        muDic[@"userId"] = [USER_DEFAULTS objectForKey:@"userid"];
        muDic[@"token"] = [USER_DEFAULTS objectForKey:@"token"];
        muDic[@"title"] = self.supplyView.titleTextField.text;
        muDic[@"industry"] = self.supplyView.industryTextField.text;
        muDic[@"address"] = self.supplyView.adressTextField.text;
        if (self.supplyView.priceTextField.text.length == 0) {
            muDic[@"money"] = @"0";
        }else{
            muDic[@"money"] = self.supplyView.priceTextField.text;
        }
        muDic[@"unit"] = self.supplyView.unitTextField.text;
        muDic[@"number"] = self.supplyView.quantityTextField.text;
        muDic[@"content"] = self.supplyView.describeTextView.text;
        muDic[@"endTime"] = self.supplyView.stopTimeTextField.text;
        
       
        NSString *urlStr = @"addSupply_app_chain.im.lensum";
        [LSHttpTool sendHttpData:urlStr params:muDic success:^(id responseObject) {
            NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
            [self showRightWithTitle:dicInfo[@"msg"] autoCloseTime:2];
            

//            if ([dicInfo[@"result"] integerValue] == 1) {
//                [MRProgressOverlayView showSuccessWithStatus:dicInfo[@"msg"]];
//                [self.navigationController popViewControllerAnimated:YES];
//            }else{
//                [MRProgressOverlayView showSuccessWithStatus:dicInfo[@"msg"]];
//            }

            
        } failure:^(NSError *error) {

            [self showSingleIconBubble:@"未搜索到资源"];
        }];
        
    }
}


//别忘记销毁
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:UITextViewTextDidChangeNotification];
}

@end
